package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.domain.customer.CustomerDO;
import com.qk.dto.customer.CustomerAddDTO;
import com.qk.dto.customer.CustomerDTO;
import com.qk.dto.customer.CustomerUpdateDTO;
import com.qk.entity.Customer;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.service.CustomerService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @Author ZgYn
 * @Date 2025/10/16 16:04
 * @Name CustomerServiceImpl
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
    @Override
    public PageResult<CustomerDO> selectList(CustomerDTO dto) {
        Page<Object> page = new Page<>(dto.getPage(), dto.getPageSize());
        Page<CustomerDO> customerPage = this.baseMapper.selectCustomerByPage(page, dto);
        return PageResult.<CustomerDO>builder()
                .total(Long.valueOf(customerPage.getTotal()).intValue())
                .rows(customerPage.getRecords())
                .build();
    }

    @Override
    public void addCustomer(CustomerAddDTO dto) {
        if (ObjectUtil.isEmpty(dto.getPhone())||ObjectUtil.isEmpty(dto.getName())) {
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        Customer customer = BeanUtil.copyProperties(dto, Customer.class);
        customer.setUpdateTime(LocalDateTime.now());
        customer.setCreateTime(LocalDateTime.now());
        this.baseMapper.insert(customer);
    }

    @Override
    public void updateCustomer(CustomerUpdateDTO dto) {
        if (ObjectUtil.isEmpty(dto.getPhone())||ObjectUtil.isEmpty(dto.getName())) {
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        Customer customer = BeanUtil.copyProperties(dto, Customer.class);
        customer.setUpdateTime(LocalDateTime.now());
        this.baseMapper.updateById(customer);
    }
}
